<?php 
	session_start();
	require("connect.php");
?>
<html><!-- InstanceBegin template="/Templates/temp02.dwt.php" codeOutsideHTMLIsLocked="false" -->
<style type="text/css">
<!--
#subcontent {
	position:absolute;
	left:243px;
	top:0px;
	width:706px;
	height:59px;
	z-index:1;
	background-color: #70a972;
}
#subcontent01 {
	position:absolute;
	left:22px;
	top:11px;
	width:287px;
	height:39px;
	font:Tahoma;
	font-size:30px;
	font-weight:600px;
	color:#E8E8E8;
	z-index:1;
}
#subcontent02 {
	position:absolute;
	left:312px;
	top:29px;
	font:Tahoma;
	font-size:18px;
	font-weight:500px;
	color:#E8E8E8;
	width:199px;
	height:25px;
	z-index:2;
}
-->
</style>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- InstanceBeginEditable name="doctitle" -->
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="css/product.css" />

<!-- InstanceEndEditable -->
<!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
</head>

<body>
<?php 
function GetQuantity($id){
		$cart = $_SESSION["CART"];
		$quantity=0;
		foreach(array_keys($cart) as $value){
			if($value == $id){
				$quantity = $cart[$id];
				break;
			}
		}
		return $quantity;	
	} 
?>
<div id="wrap">
	<div id="title">
		<div id="title01">
			<span><a href="register.php">Register </a>|<a href="mycart.php"> My Cart </a>|<a href="login.php"> Login </a></span>
		</div>
	</div>
	<div id="header">
		<div id="searchform">
	    	<form action="Templates/search.php">
					<input type="text" name="s" value="" onKeyPress="this.style.backgroundColor='pink'" onBlur="this.style.backgroundColor='white'" onFocus="this.style.backgroundColor='pink'"/>
			</form>
		</div>
	</div>
	<div id="menu">
		<ul>
			<li><a href="index.php">TRANG CHỦ</a></li>
			<li><a href="introduce.php">GIỚI THIỆU</a></li>
			<li><a href="product_view.php">SẢN PHẨM</a></li>
			<li><a href="promotion.php">KHUYẾN MẠI</a></li>			
			<li><a href="tuvan.php">TƯ VẤN THIẾT BỊ</a></li>
			<li><a href="phanhoi.php">PHẢN HỒI</a></li>
		</ul>
   </div>
   <div id="submenu">
   		<div id="submennu01"><a href="#">Uy tín/Chất lượng</a></div>
  </div>
   <div id="center">
   		<div id="left">
			<div id="danhmuc"><a href="product_view.php?catid=0">DANH MỤC SẢN PHẨM</a></div>
			<div id="left01">
			<?php 
						$sql=mysql_query("select * from catagory");
						while($row=mysql_fetch_array($sql))
					{
					    echo "<font color=#F8F8F8>::</font>";
						echo "<a href='product_view.php?catid={$row["catID"]}'>{$row["catName"]}</a><br>";
						echo "<font color=#666633 >-------------------------------</font><br>";
					}
			?>
			</div>
			<div id="giohang">
				<div id="giohang01"><b style="font:Tahoma; font-size:14px; color:#FFFFFF;">GIỎ HÀNG CỦA BẠN</b></div>
			</div>
			<div id="cart">
			  <?php include("cartdetail.php");?></div>
		</div>
		<div id="subcontent">
			<div id="subcontent01">NỘI THẤT CAO CẤP</div>
			<div id="subcontent02">UY TÍN - CHẤT LƯỢNG</div>
		</div>
		<div id="content"><!-- InstanceBeginEditable name="vungsuaduoc" -->
		<?php 
	$cart = $_SESSION["CART"];
// Kiem tra xem co hang trong gio hang khong 
if( $_SESSION["userlogin"]!=true){
		$_SESSION["login_error"]="Phai dang nhap";
		echo("<script>window.location='login.php'</script>");
	}
else{
	if($cart){
			$idlist = "";
			$total=0;
			$a = $_REQUEST["t1"];
			$b = $_REQUEST["t2"];
			$c = $_REQUEST["t3"];
			//xem ai la nguoi mua hang
			$uid = $_SESSION["uid"];
			//$uid = $_REQUEST["uid"];
			//neu ko nhap thong tin thi lay tu bang khach hang
			if ($a=="" && $b == "" && $c==""){
			$sql1 = "select * from user where uid = $uid";
			$rs = mysql_query($sql1) or die("error ".$sql1);
			$r = mysql_fetch_array($rs);
			$sql = "INSERT INTO `order` (uID,oDate,oGetName,oGetAddress,oGetTel,oStatus) VALUES($uid,Date(Now()),'{$r["uname"]}','{$r["address"]}','{$r["phone"]}',0)";
			} else 
				
				//Tao 1 hoa don moi
				$sql = "INSERT INTO `order` (uID,oDate,oGetName,oGetAddress,oGetTel,oStatus) VALUES($uid,Date(Now()),'{$a}','{$b}','{$c}',0)";
						
						mysql_query($sql);
						
						//Lay orderID LIMIT 0,1 la lay ban ghi tu vi tri 0 tiep theo 1 ban ghi. Cau lenh nay thay lenh TOP trong MSSQL
						
						$sql = "SELECT oID FROM `order` ORDER BY oID DESC LIMIT 0,1";
						
						$result = mysql_query($sql) or die("Loi chen lay du lieu");
						while( $row = mysql_fetch_array($result)){
							$orderid = $row[0];
						}
						
						//Insert vao bang Order Detail
						
						foreach(array_keys($cart) as $value){
							
							//Lay gia ban 
							$sql = "SELECT * FROM product WHERE pID = $value";
							
							$result = mysql_query($sql);
							while($row = mysql_fetch_array($result)){
								$price = $row["pPrice"];
							}
							//Insert du lieu vao order detail
							
							$sql = "INSERT INTO `orderdetail` (oID,pID,odQuantity,odPrice) VALUES($orderid,$value,".GetQuantity($value).",$price)";
							
							mysql_query($sql) or die("Lỗi chèn chi tiết".$sql);
						}
						
						//mysql_close();
						//Xoa gio hang khi insert du lieu xong
						$_SESSION["CART"] = "";
						echo("<font tahoma color=#003300><h3><b><center>Cám ơn bạn đã mua hàng Chúng tôi sẽ liên hệ với bạn <br>trong thời gian sớm nhất!!</center></h3></font>");
						echo "<script language=javascript> setTimeout('document.location=\'index.php\'',3000);</script>";													
	}else{
		echo "<font tahoma color=red><h3><b><center>Không có hàng trong giỏ hàng!!</h3></font></center>";
		echo "<script>setTimeout(\"window.location='index.php';\",4000);</script>";
	}	
}
?>
		<!-- InstanceEndEditable --></div>
   </div>
   <div id="footer01">
  		<center><a href="#">Design By H3TL&nbsp;Group3<br> Mọi thông tin chi tiết xin liên hệ: h3tl.itpro@gmail.com</a></center>
  </div>
   
</div>


</body>
<!-- InstanceEnd --></html>
